package com.swxc.smartorder.utils

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*

/**
 * 日期操作工具类.
 *
 * @author
 */

@SuppressLint("SimpleDateFormat")
object DateUtil {


    /**
     * 获取App要求的格式化时间
     * 在当天      显示     今天 04:40
     * 在昨天      显示     昨天 23:12
     * 超过2天     显示     01-11 21:09
     * 不在本年    显示     2011-11-11 01:11
     */
    fun getTimeFormatString(timeLong: Long): String {

        return when {
            isToday(timeLong) -> "今天 ${getCustomFormat(timeLong, "HH:mm")}"
            isYesterday(timeLong) -> "昨天 ${getCustomFormat(timeLong, "HH:mm")}"
            isThisYear(timeLong) -> getCustomFormat(timeLong, "MM-dd HH:mm")
            else -> getCustomFormat(timeLong, "yyyy-MM-dd HH:mm")
        }


    }

    private const val INTERVAL_IN_MILLISECONDS = (30 * 1000).toLong()
    /**
     * 默认时间格式
     */
    private const val FORMAT = "yyyy-MM-dd HH:mm:ss"
    private const val SS = 1000
    private const val MI = SS * 60
    private const val HH = MI * 60
    private const val DD = HH * 24

    /**
     * 获取当前时间   String
     */
    val curDateStr: String
        get() {
            val c = Calendar.getInstance()
            c.time = Date()
            return (c.get(Calendar.YEAR).toString() + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)
                    + ":" + c.get(Calendar.SECOND))
        }

    private val yesterdayStartAndEndTime: TimeInfo
        get() {
            val calendar1 = Calendar.getInstance()
            calendar1.add(Calendar.DATE, -1)
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)

            val startDate = calendar1.time
            val startTime = startDate.time

            val calendar2 = Calendar.getInstance()
            calendar2.add(Calendar.DATE, -1)
            calendar2.set(Calendar.HOUR_OF_DAY, 23)
            calendar2.set(Calendar.MINUTE, 59)
            calendar2.set(Calendar.SECOND, 59)
            calendar2.set(Calendar.MILLISECOND, 999)
            val endDate = calendar2.time
            val endTime = endDate.time
            val info = TimeInfo()
            info.startTime = startTime
            info.endTime = endTime
            return info
        }

    val todayStartAndEndTime: TimeInfo
        @SuppressLint("SimpleDateFormat")
        get() {
            val calendar1 = Calendar.getInstance()
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)
            val startDate = calendar1.time
            val startTime = startDate.time
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss S")

            val calendar2 = Calendar.getInstance()
            calendar2.set(Calendar.HOUR_OF_DAY, 23)
            calendar2.set(Calendar.MINUTE, 59)
            calendar2.set(Calendar.SECOND, 59)
            calendar2.set(Calendar.MILLISECOND, 999)
            val endDate = calendar2.time
            val endTime = endDate.time
            val info = TimeInfo()
            info.startTime = startTime
            info.endTime = endTime
            return info
        }

    val beforeYesterdayStartAndEndTime: TimeInfo
        get() {
            val calendar1 = Calendar.getInstance()
            calendar1.add(Calendar.DATE, -2)
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)
            val startDate = calendar1.time
            val startTime = startDate.time

            val calendar2 = Calendar.getInstance()
            calendar2.add(Calendar.DATE, -2)
            calendar2.set(Calendar.HOUR_OF_DAY, 23)
            calendar2.set(Calendar.MINUTE, 59)
            calendar2.set(Calendar.SECOND, 59)
            calendar2.set(Calendar.MILLISECOND, 999)
            val endDate = calendar2.time
            val endTime = endDate.time
            val info = TimeInfo()
            info.startTime = startTime
            info.endTime = endTime
            return info
        }

    /**
     * endtime为今天
     *
     * @return
     */
    // calendar2.set(Calendar.HOUR_OF_DAY, 23);
    // calendar2.set(Calendar.MINUTE, 59);
    // calendar2.set(Calendar.SECOND, 59);
    // calendar2.set(Calendar.MILLISECOND, 999);
    val currentMonthStartAndEndTime: TimeInfo
        get() {
            val calendar1 = Calendar.getInstance()
            calendar1.set(Calendar.DATE, 1)
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)
            val startDate = calendar1.time
            val startTime = startDate.time

            val calendar2 = Calendar.getInstance()
            val endDate = calendar2.time
            val endTime = endDate.time
            val info = TimeInfo()
            info.startTime = startTime
            info.endTime = endTime
            return info
        }

    val lastMonthStartAndEndTime: TimeInfo
        get() {
            val calendar1 = Calendar.getInstance()
            calendar1.add(Calendar.MONTH, -1)
            calendar1.set(Calendar.DATE, 1)
            calendar1.set(Calendar.HOUR_OF_DAY, 0)
            calendar1.set(Calendar.MINUTE, 0)
            calendar1.set(Calendar.SECOND, 0)
            calendar1.set(Calendar.MILLISECOND, 0)
            val startDate = calendar1.time
            val startTime = startDate.time

            val calendar2 = Calendar.getInstance()
            calendar2.add(Calendar.MONTH, -1)
            calendar2.set(Calendar.DATE, 1)
            calendar2.set(Calendar.HOUR_OF_DAY, 23)
            calendar2.set(Calendar.MINUTE, 59)
            calendar2.set(Calendar.SECOND, 59)
            calendar2.set(Calendar.MILLISECOND, 999)
            calendar2.roll(Calendar.DATE, -1)
            val endDate = calendar2.time
            val endTime = endDate.time
            val info = TimeInfo()
            info.startTime = startTime
            info.endTime = endTime
            return info
        }

    /**
     * String TO  Date
     */
    private fun str2Date(str: String?, format: String? = null): Date? {
        var format = format
        if (str == null || str.isEmpty()) {
            return null
        }
        if (format == null || format.isEmpty()) {
            format = FORMAT
        }
        var date: Date? = null
        try {
            val sdf = SimpleDateFormat(format)
            date = sdf.parse(str)

        } catch (e: Exception) {
            e.printStackTrace()
        }

        return date

    }

    @JvmOverloads
    fun str2Calendar(str: String, format: String? = null): Calendar? {

        val date = str2Date(str, format) ?: return null
        val c = Calendar.getInstance()
        c.time = date

        return c

    }

    /**
     * Date to Str
     */
    private fun date2Str(c: Calendar?, format: String? = null): String? {
        return if (c == null) {
            null
        } else date2Str(c.time, format)
    }

    @JvmOverloads
    fun date2Str(d: Date?, format: String? = null): String? {// yyyy-MM-dd HH:mm:ss
        var format = format
        if (d == null) {
            return null
        }
        if (format == null || format.isEmpty()) {
            format = FORMAT
        }
        val sdf = SimpleDateFormat(format)
        return sdf.format(d)
    }

    /**
     * 获得当前日期的字符串格式
     *
     * @param format
     * @return
     */
    fun getCurDateStr(format: String): String? {
        val c = Calendar.getInstance()
        return date2Str(c, format)
    }

    /**
     * 将时间格式到秒
     *
     * @param time
     * @return
     */
    fun getSecond(time: Long): String {

        return SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(time)

    }

    // 格式到天
    fun getDay(time: Long): String {

        return SimpleDateFormat("yyyy-MM-dd").format(time)

    }

    // 格式到毫秒
    fun getSMillon(time: Long): String {
        return SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time)
    }

    /**
     * 获取自定义的时间格式
     *
     * @param time   时间
     * @param format 格式
     * @return string
     */
    fun getCustomFormat(time: Long, format: String): String {
        return SimpleDateFormat(format).format(time)
    }

    /**
     * 根据给定的时间字符串，返回年月日
     *
     * @param allDate like "yyyy-MM-dd"
     * @return
     */
    fun getY_M_D(allDate: String?): String? {
        return if (allDate == null || allDate.length < 10) {
            allDate
        } else allDate.substring(0, 10)
    }

    fun checkValidTime(time: String?, begTime: String, endTime: String): Boolean {
        return if (time == null) false else time.compareTo(begTime) >= 0 && time.compareTo(endTime) <= 0
    }

    fun getTimestampString(dateString: String): String? {
        val date = str2Date(dateString)
        return getTimestampString(date)
    }

    fun getTimestampString(messageDate: Date?): String? {
        try {
            val curLocale = Locale.CHINA
            val languageCode = curLocale.language
            val isChinese = languageCode.contains("zh")

            var format: String? = null
            val messageTime = messageDate!!.time
            if (isToday(messageTime)) {
                val calendar = GregorianCalendar.getInstance()
                calendar.time = messageDate
                val hour = calendar.get(Calendar.HOUR_OF_DAY)

                format = "HH:mm"

                if (hour > 17) {
                    if (isChinese) {
                        format = "晚上 hh:mm"
                    }

                } else if (hour >= 0 && hour <= 6) {
                    if (isChinese) {
                        format = "凌晨 hh:mm"
                    }
                } else if (hour > 11 && hour <= 17) {
                    if (isChinese) {
                        format = "下午 hh:mm"
                    }

                } else {
                    if (isChinese) {
                        format = "上午 hh:mm"
                    }
                }
            } else if (isYesterday(messageTime)) {
                if (isChinese) {
                    format = "昨天 HH:mm"
                } else {
                    format = "MM-dd HH:mm"
                }

            } else {
                if (isChinese) {
                    format = "M月d日 HH:mm"
                } else {
                    format = "MM-dd HH:mm"
                }
            }

            return if (isChinese) {
                SimpleDateFormat(format, Locale.CHINA).format(messageDate)
            } else {
                SimpleDateFormat(format, Locale.US).format(messageDate)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }

    }


    /**
     * 判断时间是否是今天
     */
    fun isToday(time: Long): Boolean {
        return isThisTime(time, "yyyy-MM-dd")
    }

    /**
     * 判断时间是否是本年
     */
    fun isThisYear(time: Long): Boolean {
        return isThisTime(time, "yyyy")
    }

    /**
     * 判断时间是否是本月
     */
    fun isThisMonth(time: Long): Boolean {
        return isThisTime(time, "yyyy-MM")
    }

    private fun isThisTime(time: Long, pattern: String): Boolean {
        val date = Date(time)
        val sdf = SimpleDateFormat(pattern)
        val param = sdf.format(date)//参数时间
        val now = sdf.format(Date())//当前时间
        return param == now
    }

    private fun isYesterday(inputTime: Long): Boolean {
        val yStartAndEndTime = yesterdayStartAndEndTime
        return inputTime > yStartAndEndTime.startTime && inputTime < yStartAndEndTime.endTime
    }

    fun isCloseEnough(time1: Long, time2: Long): Boolean {
        // long time1 = date1.getTime();
        // long time2 = date2.getTime();
        var delta = time1 - time2
        if (delta < 0) {
            delta = -delta
        }
        return delta < INTERVAL_IN_MILLISECONDS
    }

    fun getStartTimeStr(beginTime: Date?): String? {
        var str: String? = ""
        if (beginTime != null) {
            val begin = beginTime.time
            val now = System.currentTimeMillis()
            if (begin < now) {
                if (begin == now) {
                    str = " 刚刚"
                } else {
                    val chaju = now - begin
                    if (chaju < 60 * 1000) {
                        str = " 刚刚"
                    } else {
                        if (chaju < 60 * 60 * 1000) {
                            str = (chaju / 1000 / 60).toString() + " 分钟前"
                        } else {
                            if (chaju < 24 * 60 * 60 * 1000) {
                                str = (chaju / 1000 / 60 / 60).toString() + " 小时前"
                            } else {
                                if (chaju < 7 * 24 * 60 * 60 * 1000) {
                                    str = (chaju / 1000 / 60 / 60 / 24).toString() + " 天前"
                                } else {
                                    str = DateUtil.date2Str(beginTime, "yyyy-MM-dd")
                                }
                            }
                        }
                    }
                }
            } else {
                str = "刚刚"
            }
        }
        return str
    }


    /**
     * 时间信息类
     */
    class TimeInfo {
        var startTime: Long = 0
        var endTime: Long = 0
    }

    fun isThisYear(date: Date?): Boolean {
        val sdf = SimpleDateFormat("yyyy")
        val param = sdf.format(date)//参数时间
        val now = sdf.format(Date())//当前时间
        return param == now
    }

    fun getToday(): String? {

        val day = Date()

        val df = SimpleDateFormat("yyyy-MM-dd")

        return df.format(day)
    }
}

